package com.blt.car.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

import blt.app_core.log.Logger;

/**
 * 基础适配器
 *
 * @param <T>
 * @author xgp
 */
public abstract class IBaseAdapter<T> extends BaseAdapter {
    private static final Object TAG = "YBaseAdapter";
    protected Context context;
    protected List<T> list;//数据集合
    protected LayoutInflater listContainer;//视图容器
    protected int itemViewResource;//自定义项视图源id

    public IBaseAdapter(Context context, int resource) {
        this.context = context;
        this.listContainer = LayoutInflater.from(context);
        this.itemViewResource = resource;
        list = new ArrayList<>();
    }

    public void addData(List<T> list) {
        this.list.addAll(list);
        this.notifyDataSetChanged();
    }

    public void clear(){
        this.list.clear();
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public T getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = listContainer.inflate(itemViewResource, viewGroup, false);
        }
        return getYView(i, view, viewGroup);
    }

    public abstract View getYView(int i, View view, ViewGroup viewGroup);
}